#include "widget.h"

#include <QDebug>
#include <QFile>
#include <string>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("Shortcut Creator");
    resize(400, sizeHint().height());

    nameLabel = new QLabel("Name");
    nameLabel->setFixedWidth(80);
    nameInput = new QLineEdit();
    QHBoxLayout *nameHBox = new QHBoxLayout();
    nameHBox->addWidget(nameLabel);
    nameHBox->addWidget(nameInput);

    execLable = new QLabel("Exec");
    execLable->setFixedWidth(80);
    execInput = new QLineEdit();
    QHBoxLayout *execHBox = new QHBoxLayout();
    execHBox->addWidget(execLable);
    execHBox->addWidget(execInput);

    iconLabel = new QLabel("Icon");
    iconLabel->setFixedWidth(80);
    iconInput = new QLineEdit();
    QHBoxLayout *iconHBox = new QHBoxLayout();
    iconHBox->addWidget(iconLabel);
    iconHBox->addWidget(iconInput);

    typeLabel = new QLabel("Type");
    typeLabel->setFixedWidth(80);
    typeInput = new QLineEdit("Application");
    QHBoxLayout *typeHBox = new QHBoxLayout();
    typeHBox->addWidget(typeLabel);
    typeHBox->addWidget(typeInput);

    categoriesLable = new QLabel("Categories");
    categoriesLable->setFixedWidth(80);
    categoriesInput = new QLineEdit();
    QHBoxLayout *categoriesHBox = new QHBoxLayout();
    categoriesHBox->addWidget(categoriesLable);
    categoriesHBox->addWidget(categoriesInput);

    savePathLable = new QLabel("Save to");
    savePathLable->setFixedWidth(80);
    savePathInput = new QLineEdit();
    savePathInput->setText("/usr/share/applications/");
    QHBoxLayout *savePathHBox = new QHBoxLayout();
    savePathHBox->addWidget(savePathLable);
    savePathHBox->addWidget(savePathInput);

    outTextLable = new QLabel("Out text");
    outTextLable->setFixedWidth(80);
    outText = new QTextEdit();
    QHBoxLayout *outTextHBox = new QHBoxLayout();
    outTextHBox->addWidget(outTextLable);
    outTextHBox->addWidget(outText);

    saveButton = new QPushButton("Create");
    connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(handleSave()));
    QHBoxLayout *footerHBox = new QHBoxLayout();
    footerHBox->addStretch();
    footerHBox->addWidget(saveButton);

    QVBoxLayout *vlay = new QVBoxLayout();
    vlay->addLayout(nameHBox);
    vlay->addLayout(execHBox);
    vlay->addLayout(iconHBox);
    vlay->addLayout(typeHBox);
    vlay->addLayout(categoriesHBox);

    vlay->addLayout(savePathHBox);
//    vlay->addLayout(outTextHBox);
    vlay->addLayout(footerHBox);

    setLayout(vlay);

}

Widget::~Widget()
{

}

void Widget::handleSave()
{

    QString name = nameInput->text();
    QString exec = execInput->text();
    QString icon = iconInput->text();
    QString type = typeInput->text();
    QString categories = categoriesInput->text();

    QString savePath = savePathInput->text();


    // example: QFile file("/home/faith/Documents/test.desktop");
    QString filePath = savePath + "/" + name + ".desktop";
    QFile file(filePath);
    bool openStatus = file.open(QIODevice::ReadWrite | QIODevice::Text);
    qDebug() << "openStatus:" << openStatus;

    QTextStream out(&file);
    out << "[Desktop Entry]\n";
    out << "Name=" << name << "\n";
    out << "Exec=" << exec << "\n";
    out << "Icon=" << icon << "\n";
    out << "Type=" << type << "\n";
    out << "Categories=" << categories << "\n";

    file.close();

    qDebug() << "File written successfully.";

}

